import dayjs from 'dayjs'

declare module 'dayjs' {
  interface Dayjs {
    /** 智能格式化 */
    $formatSmart(template: string | null | undefined): string
  }
}

dayjs.extend((_o, c) => {
  c.prototype.$formatSmart = function (template) {
    if (!template) {
      return this.format()
    }

    const today = dayjs()
    let tpl = template

    // 今年
    if (this.year() === today.year()) {
      tpl = tpl.replace(new RegExp('(^YYYY[-/.年])|(/YYYY$)'), '')
    }

    return this.format(tpl)
  }
})
